/**
 * Created by Wing on 2017/5/23.
 */
var beforeeditNote = function () {
    var noteId;// = $("#noteId").val();
    if (noteId == undefined || noteId == "") {
        var selecteds = $(".thumbnail.selected");

        if (selecteds.length > 1) {
            showTip("一次只能编辑一个笔记", undefined, false);
            return;
        } else if(selecteds.length == 0) {
            showTip("请选择一个笔记进行编辑", undefined, false);
            return;
        }else{
                noteId = $(selecteds).attr("value");
                $("#editNoteA span").click();
            $("#noteId").val(noteId);

        }
    }
    var deckId;

    // 更新标题和区域内容
    $.getJSON(
        "/memorypalace/note/getnotebynoteid.action?noteId=" + noteId,
        function (note) {
            var areaNamesArray = JSON.parse(note.areaNames);
            var areaContentsMap = JSON.parse(note.areaContents);
            $("#areaContentId").html('');
            var h = "";
            $.each(areaNamesArray, function (i, areaName) {
                var content = areaContentsMap[i];
                // 标题
                h += "<h4>" + areaName + "</h4>";
                // 输入框
                h += "<textarea name='" + areaName + "' id='textarea" + i + "'>";
                h += content;
                h += "</textarea>";
            });
            $("#areaContentId").html(h);
            // 更新牌组
            $("#deckNameId").html(note.deck.deckName + '<span class="caret" style="margin-left:0.5em"></span>');
            $("#deckNameId").val(note.deck.deckId);
            // 更新类型
            $("#noteTypeId").html(note.noteType.noteTypeName + '<span class="caret" style="margin-left:0.5em"></span>');
            $("#noteTypeId").val(note.noteType.noteTypeId);


        }
    );

    // 更新类型"下拉菜单"
    $.getJSON(
        "/memorypalace/notetype/getmynotetype.action",
        function (notetypeList) {
            // 初始化
            $("#noteTypeIdList").html('');
            $.each(notetypeList, function (i, noteType) {
                $("#noteTypeIdList").append(
                    "<li><a href=\"javascript:changeDropDownBtnValue('" + noteType.noteTypeName + "','noteTypeId','" + noteType.noteTypeId + "');changeNoteType('" + noteType.noteTypeId + "');" +
                    "\">" + noteType.noteTypeName + "</a></li>"
                );
            });
        }
    );

    // 更新牌组"下拉菜单"
    $.getJSON(
        "/memorypalace/knowledge/getmydeck.action",
        function (deckList) {
            // 初始化
            $("#deckNameIdList").html('');
            $.each(deckList, function (i, deck) {
                $("#deckNameIdList").append(
                    "<li><a href=\"javascript:changeDropDownBtnValue('" + deck.deckName + "','deckNameId','" + deck.deckId + "');" +
                    "\">" + deck.deckName + "</a></li>"
                );
            });
        }
    );


    $("#editNoteHBox h2").html("编辑笔记");
    $("#editNoteConfirmBtn").off();
    $("#editNoteConfirmBtn").on("click", toeditNote);
    return false;

}
var toeditNote = function () {
    var noteId=$("#noteId").val();

    // 区域内容
    var content = [];
    // 区域名
    var areanames = [];
    $("textarea[id^=textarea]").each(function (i, textarea) {
        var key = $(textarea).attr("name");
        areanames.push(key);
        var value = $(textarea).val();
        // content[key] = value;
        content.push(value);
    });
    // 笔记类型id
    var noteTypeId = $("#noteTypeId").val();
    // 牌组id
    var deckId = $("#deckNameId").val();
    var data = {};
    data["areaContents"] = JSON.stringify(content);
    data["areaNames"] = JSON.stringify(areanames);
    data["noteTypeId"] = noteTypeId;
    data["deckId"] = deckId;
    data["noteId"] = noteId;
    $.ajax({
        'url': '/memorypalace/note/editnote.action',
        'type': 'GET',
        'data': data,
        'success': function (result) {
            if (result == 1) {
                // 刷新思维导图的数据
                // getJsMindData();
                getNoteData();
                cancelEditNote();
                showTip("编辑成功", undefined, true);
            }
        },
        'error':function () {
            showTip("编辑失败", undefined, false);
        }
    });
}
var beforeAddNote = function () {
    $("#editNoteHBox h2").html("新增笔记");

    // 更新类型"下拉菜单"
    $.getJSON(
        "/memorypalace/notetype/getmynotetype.action",
        function (notetypeList) {
            $("#noteTypeId").val(notetypeList[0].noteTypeId);
            $("#noteTypeId").html(notetypeList[0].noteTypeName + '<span class="caret" style="margin-left:0.5em"></span>');
            // 初始化下拉
            $("#noteTypeIdList").html('');
            $.each(notetypeList, function (i, noteType) {
                $("#noteTypeIdList").append(
                    "<li><a href=\"javascript:changeDropDownBtnValue('" + noteType.noteTypeName + "','noteTypeId','" + noteType.noteTypeId + "');changeNoteType('" + noteType.noteTypeId + "');" +
                    "\">" + noteType.noteTypeName + "</a></li>"
                );
            });
            // 区域名和区域输入框
            changeNoteType(notetypeList[0].noteTypeId,1);
        }
    );

    // 更新牌组"下拉菜单"
    $.getJSON(
        "/memorypalace/knowledge/getmydeck.action",
        function (deckList) {

            // 初始化
            $("#deckNameIdList").html('');
            $.each(deckList, function (i, deck) {
                // 标题
                // 牌组id
                if (deck.deckId == _deckId) {
                    $("#deckNameId").val(deckList[i].deckId);
                    $("#deckNameId").html(deckList[i].deckName + '<span class="caret" style="margin-left:0.5em"></span>');
                }
                // 下拉
                $("#deckNameIdList").append(
                    "<li><a href=\"javascript:changeDropDownBtnValue('" + deck.deckName + "','deckNameId','" + deck.deckId + "');" +
                    "\">" + deck.deckName + "</a></li>"
                );
            });
        }
    );


    $("#editNoteConfirmBtn").off();
    $("#editNoteConfirmBtn").on("click", toaddNote);
    $("#addNoteA span").click();
}
var toaddNote = function () {
    // 区域内容
    var content = [];
    // 区域名
    var areanames = [];
    $("textarea[id^=textarea]").each(function (i, textarea) {
        var key = $(textarea).attr("name");
        areanames.push(key);
        var value = $(textarea).val();
        // content[key] = value;
        content.push(value);
    });
    // 笔记类型id
    var noteTypeId = $("#noteTypeId").val();
    // 牌组id
    var deckId = $("#deckNameId").val();
    var data = {};
    data["areaContents"] = JSON.stringify(content);
    data["areaNames"] = JSON.stringify(areanames);
    data["noteTypeId"] = noteTypeId;
    data["deckId"] = deckId;
    data["noteCreateTime"] = new Date();
    data["noteEditTime"] = new Date();
    $.getJSON(
        "/memorypalace/note/addnote.action",
        data,
        function (flag) {
            if (flag) {
                showTip("添加成功", undefined, true);
                cancelEditNote();
                getNoteData();
            } else {
                showTip("添加失败", undefined, false);
                // cancelEditNote();
            }
        }
    );
}
var changeNoteType = function (noteTypeId,empty) {
    console.log(noteTypeId);
    $.getJSON(
        "/memorypalace/notetype/getnotetypebyid.action?noteTypeId=" + noteTypeId,
        function (noteType) {
            var areaNamesArray = JSON.parse(noteType.areaNames);
            // 1.拿到已输入的数据
            var textareaIndex = 0;
            var textArray = [];
            while ($("#textarea" + textareaIndex).attr("id") != undefined) {
                textArray.push($("#textarea" + textareaIndex).val());
                textareaIndex++;
            }
            // 2.切换区域名,输入框的内容不变
            $("#areaContentId").html('');
            var h = "";
            $.each(areaNamesArray, function (i, areaName) {
                // 标题
                h += "<h4>" + areaName + "</h4>";
                if (i < textArray.length) {
                    if(empty == undefined) {
                        var content = textArray[i];
                    }else{
                        var content = "";
                    }

                    // 输入框
                    h += "<textarea name='" + areaName + "' id='textarea" + i + "'>";
                    h += content;
                    h += "</textarea>";
                } else {
                    // 输入框
                    h += "<textarea name='" + areaName + "' id='textarea" + i + "'>";
                    h += "</textarea>";
                }
            })
            $("#areaContentId").html(h);
        }
    );
}

var cancelEditNote = function () {
    $('#editNoteHBox .md-close span').click();
}

var deleteNote = function () {
    var selecteds = $(".thumbnail.selected");
    var noteId;
    if (selecteds.length > 1) {
        showTip("一次只能删除一个笔记", undefined, false);
        return;
    } else if(selecteds.length == 0) {
        showTip("请选择一个笔记进行编辑", undefined, false);
        return;
    }else{
        noteId = $(selecteds).attr("value");
    }
    $.ajax({
        url:"/memorypalace/note/deletenote.action",
        type:"post",
        data:{"noteId":noteId},
        success:function (deleteFlag) {
            if(deleteFlag) {
                showTip("删除成功", undefined, true);
                getNoteData();
            }else{
                showTip("删除失败",undefined,false);
            }
        },
        error:function () {
            showTip("删除失败",undefined,false);
        }
    });
    return false;
}